﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace EIA.WCFService.LogicalService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“Service1”。
    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.PerCall,
        ConcurrencyMode = ConcurrencyMode.Reentrant,
        UseSynchronizationContext = false)]
    public class Service : ILogicalService
    {
        public string Call(string business, string func, string args)
        {
            string[] strArgs = null;
            try
            {
                strArgs = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(args);
            }
            catch
            {
                throw new FaultException("Arguments error!");
            }
            try
            {
                string ret = Processor.Process(business, func, strArgs);
                return ret;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public System.IO.Stream GetClientAccessPolicy()
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            System.IO.StreamWriter sw = new System.IO.StreamWriter(ms, Encoding.UTF8); ;
            sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\"?><access-policy><cross-domain-access><policy><allow-from http-request-headers=\"*\"><domain uri=\"*\"/></allow-from><grant-to><resource path=\"/\" include-subpaths=\"true\"/></grant-to></policy></cross-domain-access></access-policy>");
            sw.Flush();
            ms.Position = 0;
            return ms;
        }

        static Service()
        {
            Processor = new DotNETX.Business.Module.MefExtension.MefBusinessProcessor();
            try
            {
                Processor.LoadModules();
            }
            catch
            {
            }
        }
        static DotNETX.Business.Module.MefExtension.MefBusinessProcessor Processor
        {
            get;
            set;
        }
    }
}
